<template>
  <div class="gas-fee-management">


    <!-- 查询条件 -->
    <el-card class="search-card">
      <el-form :model="queryForm" inline>
        <el-form-item label="社区">
          <el-select v-model="queryForm.communityId" placeholder="请选择社区" clearable>
            <el-option label="测试社区" :value="1" />
          </el-select>
        </el-form-item>
        <el-form-item label="缴费状态">
          <el-select v-model="queryForm.paymentStatus" placeholder="请选择缴费状态" clearable>
            <el-option label="未缴费" :value="0" />
            <el-option label="已缴费" :value="1" />
            <el-option label="逾期" :value="2" />
          </el-select>
        </el-form-item>
        <el-form-item label="费用类型">
          <el-select v-model="queryForm.feeType" placeholder="请选择费用类型" clearable>
            <el-option label="燃气费" :value="FEE_TYPE.GAS_FEE" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">查询</el-button>
          <el-button @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 操作栏 -->
    <el-card class="operation-card">
      <el-row :gutter="10">
        <el-col :span="1.5">
          <el-button type="primary" @click="handleMeterReading">
            <el-icon><Edit /></el-icon>
            表读数录入
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="success" @click="handleGenerate">
            <el-icon><Plus /></el-icon>
            生成账单
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="warning" @click="handleSafetyCheck">
            <el-icon><Warning /></el-icon>
            安全检查
          </el-button>
        </el-col>
      </el-row>
    </el-card>

    <!-- 燃气费列表 -->
    <el-card class="table-card">
      <el-table v-loading="tableLoading" :data="gasFeeList" stripe>
        <el-table-column prop="id" label="账单ID" width="80" />
        <el-table-column prop="userName" label="用户姓名" min-width="120" />
        <el-table-column prop="householdAddress" label="房户地址" min-width="200" />
        <el-table-column prop="amount" label="费用金额" width="120">
          <template #default="{ row }">
            <span class="amount-text">¥{{ row.amount }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="billingMonth" label="计费月份" width="120" />
        <el-table-column label="费用类型" width="100">
          <template #default="{ row }">
            <el-tag type="info">燃气费</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="缴费状态" width="100">
          <template #default="{ row }">
            <el-tag :type="getTagType(getPaymentStatusTagType(row.paymentStatus))">
              {{ getPaymentStatusText(row.paymentStatus) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200" fixed="right">
          <template #default="{ row }">
            <el-button 
              v-if="row.paymentStatus === 0"
              type="primary" 
              size="small" 
              @click="handlePay(row)"
            >
              立即缴费
            </el-button>
            <el-button size="small" @click="handleViewDetail(row)">
              查看详情
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination-container">
        <el-pagination
          v-model:current-page="pagination.current"
          v-model:page-size="pagination.size"
          :page-sizes="[10, 20, 50, 100]"
          :total="pagination.total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted, watch } from 'vue'
import { showMessage, getTagType } from '@/utils/message'
import { Edit, Plus, Warning } from '@element-plus/icons-vue'
import { gasFeeApi } from '@/api/property'
import type { GasFee, BaseQueryParams } from '@/views/property/types/property'
import { FEE_TYPE } from './types/property'

const tableLoading = ref(false)
const gasFeeList = ref<GasFee[]>([])

const pagination = reactive({
  current: 1,
  size: 10,
  total: 0
})

const queryForm = reactive({
  communityId: undefined as number | undefined,
  feeType: FEE_TYPE.GAS_FEE as FEE_TYPE,
  paymentStatus: undefined as number | undefined,
  billingMonth: ''
})

// 费用类型下拉（如后续支持多类型可扩展）
const feeTypeOptions = [
  { label: '燃气费', value: FEE_TYPE.GAS_FEE }
]

watch(() => queryForm.feeType, (val) => {
  // 切换费用类型时可自动重置其它筛选条件
  // queryForm.billingMonth = ''
})

const getGasFeeList = async () => {
  try {
    tableLoading.value = true
    const params: BaseQueryParams & { feeType?: FEE_TYPE } = {
      current: pagination.current,
      size: pagination.size,
      ...queryForm
    }
    
    const response = await gasFeeApi.getGasFeeList(params)
    if (response.code === 200) {
      gasFeeList.value = response.data.records
      pagination.total = response.data.total
    }
  } catch (error) {
    console.error('获取燃气费列表失败:', error)
    showMessage({ type: 'error', message: '获取燃气费列表失败' })
  } finally {
    tableLoading.value = false
  }
}

const handleSearch = () => {
  pagination.current = 1
  getGasFeeList()
}

const handleReset = () => {
  Object.assign(queryForm, {
    communityId: undefined,
    paymentStatus: undefined,
    billingMonth: ''
  })
  handleSearch()
}

const handleMeterReading = () => {
  showMessage({ type: 'info', message: '表读数录入功能开发中...' })
}

const handleGenerate = () => {
  showMessage({ type: 'info', message: '生成账单功能开发中...' })
}

const handleSafetyCheck = () => {
  showMessage({ type: 'info', message: '安全检查功能开发中...' })
}

const handlePay = (row: GasFee) => {
  showMessage({ type: 'info', message: '缴费功能开发中...' })
}

const handleViewDetail = (row: GasFee) => {
  showMessage({ type: 'info', message: '查看详情功能开发中...' })
}

const handleSizeChange = (size: number) => {
  pagination.size = size
  pagination.current = 1
  getGasFeeList()
}

const handleCurrentChange = (current: number) => {
  pagination.current = current
  getGasFeeList()
}

const getPaymentStatusTagType = (status: number) => {
  switch (status) {
    case 0: return 'warning'
    case 1: return 'success'
    case 2: return 'danger'
    default: return 'info'
  }
}

const getPaymentStatusText = (status: number) => {
  switch (status) {
    case 0: return '未缴费'
    case 1: return '已缴费'
    case 2: return '逾期'
    default: return '未知'
  }
}

onMounted(() => {
  getGasFeeList()
})
</script>

<style scoped>
.gas-fee-management {
  padding: 20px;
}

.page-header {
  margin-bottom: 20px;
}

.page-header h2 {
  margin: 0 0 8px 0;
  color: #303133;
}

.page-description {
  margin: 0;
  color: #909399;
  font-size: 14px;
}

.search-card {
  margin-bottom: 16px;
}

.operation-card {
  margin-bottom: 16px;
}

.table-card {
  margin-bottom: 16px;
}

.amount-text {
  color: #e6a23c;
  font-weight: bold;
}

.pagination-container {
  margin-top: 20px;
  text-align: right;
}
</style> 